package com.hoongs.v1.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hoongs.v1.dto.SocketMsgPageDto;
import com.hoongs.v1.entity.SocketMsg;
import com.hoongs.v1.service.SocketMsgService;
import com.hoongs.v1.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/socketMsg")
@Validated
public class SocketMsgController {

    @Resource
    private SocketMsgService socketMsgService;

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result add(@Validated SocketMsg socketMsg, BindingResult bindingResult) {
        String res = Result.resultMsg(bindingResult);
        return StringUtils.isNotBlank(res) ? Result.opError(res) : socketMsgService.add(socketMsg);
    }

    @PostMapping("/remove")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result remove(String id) {
        return socketMsgService.remove(id);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result update(SocketMsg socketMsg) {
        return socketMsgService.update(socketMsg);
    }

    @GetMapping("/selectById")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectById(String id) {
        return socketMsgService.selectById(id);
    }

    @GetMapping("/selectPageList")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectPageList(SocketMsgPageDto socketMsgPageDto) {
        return socketMsgService.selectPageList(socketMsgPageDto);
    }

    @PostMapping("/read")
    public Result read(String id) {
        return socketMsgService.read(id);
    }
}
